﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SoundServant.Controls;
using System.Management;
using System.IO;

namespace SoundServant
{
    /// <summary>
    /// Interaction logic for AddCD.xaml
    /// </summary>
    public partial class CopyMP3Window : SSWindow
    {
        StoredRecording recording;
        Burner max;

        public CopyMP3Window(StoredRecording _r)
        {
            try
            {
                recording = _r;

                if (recording.Current)
                {
                    SSMessageBox.Show("Error", "You must stop recording before you can copy this recording as an MP3");
                    this.Close();
                }

                InitializeComponent();
                RecordingViewHolder.Content = new RecordingReadOnlyControl(recording);
                PopulateDriveSelector();

            }
            catch (Exception e)
            {
                SSMessageBox.Show("Error", "There was an error while loading the copy MP3 Window :\r\n" + e.ToString());
            }
        }

        private void PopulateDriveSelector()
        {
            DriveSelectorListBox.Items.Clear();

            foreach (DriveInfo driveInfo in DriveInfo.GetDrives())
            {
                if ((Options.Default.RevealFixedStorage && driveInfo.DriveType == DriveType.Fixed && !driveInfo.Name.Substring(0, 1).ToLower().Equals("c")) || (driveInfo.DriveType == DriveType.Removable && driveInfo.IsReady))
                    DriveSelectorListBox.Items.Add(new DriveSelectorListBoxItem(driveInfo));
            }

            if (DriveSelectorListBox.Items.Count == 1)
            {
                DriveSelectorListBox.SelectedIndex = 0;
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void CopyMP3Button_Click(object sender, RoutedEventArgs e)
        {
            if (DriveSelectorListBox.SelectedIndex != -1)
            {
                recording.ExportMp3(((DriveSelectorListBoxItem)DriveSelectorListBox.SelectedItem).Drive.Name);
                this.Close();
            }
        }

        private void RefreshButton_Click(object sender, RoutedEventArgs e)
        {
            PopulateDriveSelector();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DriveSelectorListBox.Items.Count > 0)
            {
                DriveSelectorListBox.SelectedIndex = 1;
            }
        }

        private void DriveSelectorListBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter && DriveSelectorListBox.SelectedIndex != -1)
            {
                recording.ExportMp3(((DriveSelectorListBoxItem)DriveSelectorListBox.SelectedItem).Drive.Name);
                this.Close();
            }
        }
    }
    class DriveSelectorListBoxItem : ListBoxItem
    {
        public DriveInfo Drive
        {
            get
            {
                return driveInfo;
            }
        }
        DriveInfo driveInfo;
        public DriveSelectorListBoxItem(DriveInfo _driveInfo)
            : base()
        {
            driveInfo = _driveInfo;
            FontSize = 14D;
            Padding = new Thickness(6);
            Margin = new Thickness(6);
            Content = driveInfo.Name + " " + driveInfo.VolumeLabel;
        }
    }
}
